﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Linq;
using PaulBeck.Timesheet.BOL;

namespace PaulBeck.Timesheet.WebParts.UserProjects
{
    public partial class UserProjectsUserControl : UserControl
    {
        public int UserId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DisplayUserProjects();
            }
        }

        public void DisplayUserProjects()
        {
            try
            {
                UserProjects up = (UserProjects)this.Parent;
                if (up.provider != null)
                {
                    UserId = up.provider.GetResourceId;
                }
                string loginName = CommonWFE.CurrentUserLoginName();
                if (UserId == 0)
                {
                    UserId = ConsultantBOL.GetConsultantIdByLoginKey(loginName);    // Display the current consultant/resource/user
                }

                gvUserProjects.DataSource = ConsultantBOL.GetClientProjectHoursByConsultantId(UserId);
                gvUserProjects.DataBind();
            }
            catch (InvalidOperationException)
            {
                // Data is missing and Link to SQL query is throwing InvalidOperationException "Sequence contains no elements"

            }
        }

    }
}
